package com.hw.hcit.feature;



import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.provider.Settings;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.android.internal.statusbar.IStatusBarService;
import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;
import com.hw.hcit.conf.JTestItemCallback;

public class AutoResultTest extends JTestItem {
    private String TAG = "AutoResultTest";
    private boolean ageTested = true;
    private static boolean svNVOK = false;
    private static int svCount = 0;

    public AutoResultTest() {
        super();
    }

    public AutoResultTest(JCtrlCallback cb) {
        super(cb);
    }

    private static int failCounts = 0;
    private TextView tv;
    private ListView listView;
    private ArrayList<String> faileItems = new ArrayList<String>();
    private Button mButton;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Fragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.project, container, false);

        tv = (TextView) rootView.findViewById(R.id.textView1);
        listView = (ListView) rootView.findViewById(R.id.listView1);
        /* 15-10-24 gaoshenpan fix add button to make sure NVSaved begin */
        mButton = (Button) rootView.findViewById(R.id.button1);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            	Config.STEP = Config.UI_PROJECTS;
            	/*15-10-26 gaoshenpan fix go to original menu after NV saved in AutoResultTest begin*/
        		Config.GetActivity().getFragmentManager().beginTransaction()
        				.replace(R.id.frame_layout, Config.PROJECTS.getUI()).commit();
        		/*15-10-26 gaoshenpan fix go to original menu after NV saved in AutoResultTest end*/
            }
        });
        /* 15-10-24 gaoshenpan fix add button to make sure NVSaved end */
        return rootView;
    }

    /* 15-09-23 gaoshenpan fix result not save nv begin */
    /* 15-10-24 gaoshenpan fix show testResultUI begin */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
            case 0:
                Log.d(TAG, "handleMessage--------->0");
                listView.setVisibility(View.VISIBLE);
                Log.d(TAG, "abcd-------->write111" + "abcd".substring(1, 2));
                ArrayAdapter<String> failAdapter = new ArrayAdapter<String>(
                        Config.GetActivity(), R.layout.array_item, faileItems);
                listView.setAdapter(failAdapter);
                break;
            case 1:
                Log.d(TAG, "handleMessage--------->1");
                Log.d(TAG, "--------->4");
                tv.setVisibility(View.VISIBLE);
                Log.d(TAG, "abcd-------->write122" + "abcd".substring(1, 2));
                tv.setText(R.string.test_success);
                tv.setTextColor(Color.GREEN);
                break;
            case 2:
                Log.d(TAG, "handleMessage--------->2");
                mButton.setEnabled(true);
                mButton.setText(getString(R.string.nv_ok));
                break;
            case 3:
                Log.d(TAG, "handleMessage--------->3");
                mButton.setEnabled(true);
                mButton.setText(getString(R.string.nv_fail));
                break;
                /* 15-10-24 gaoshenpan fix show testResultUI end */
            default:
                break;
            }

        }
    };

    /* 15-09-23 gaoshenpan fix result not save nv end */

    @Override
    public boolean getResult() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public String getResultInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startTest() {
        // TODO Auto-generated method stub

    }

    @Override
    public void stopTest() {
        // TODO Auto-generated method stub

    }

    public void refresh() {

    }
    /*15-10-24 gaoshenpan fix hideNavigationBar& make leave time for NVSaved begin*/
    @Override
    public void onResume() {
        super.onResume();
        mButton.setEnabled(false);
        svNVOK = false;
        svCount = 0;
        hideNavigationBar();
        Settings.Global.putInt(Config.GetActivity().getContentResolver(),
                "mmi_test_fullscreen", 1);
        faileItems.clear();
        /* 15-10-19 gaoshenpan fix not show testresult begin */
        failCounts = 0;
        Log.d(TAG, "--------->0");
        String s = "";
        for (int i = 0; i < Config.CURR_PROJECT.getSize(); i++) {
            Log.d(TAG, "--------->1");
            if (!Config.CURR_PROJECT.getValue(i).getResult()) {
                faileItems.add(Config.CURR_PROJECT.getValue(i).getName());
                Log.d(TAG, "----1----->2");
                failCounts++;
                Log.d(TAG, "----2----->2");
                Log.d(TAG, "failCounts----1----->" + failCounts);

            }
            // s +=
            // Config.CURR_PROJECT.getValue(i).getName()+":"+Config.CURR_PROJECT.getValue(i).getResult()
            // + "<br>";
        }
        Log.d(TAG, "failCounts----2----->" + failCounts);
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    Thread.sleep(2000);
                    Log.d(TAG, "abcd----t---->write11" + "abcd".substring(1, 2));
                    saveNV();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }).start();

        /* 15-10-19 gaoshenpan fix not show testresult end */

    }

    private void saveNV() {
        svCount++;
        Log.d(TAG, "svCount--------->" + svCount);
        if (failCounts != 0) {
            Log.d(TAG, "--------->3");
            Log.d(TAG, "abcd-------->write11" + "abcd".substring(1, 2));
            if (!Config.ageingTested) {
                svNVOK = rewriteOneByteOfNv2499_1(62, 'F');
            } else {
                svNVOK = rewriteOneByteOfNv2499_1(63, 'F');
            }
            mHandler.sendEmptyMessage(0);
        } else {
            Log.d(TAG, "abcd-------->write12" + "abcd".substring(1, 2));
            if (!Config.ageingTested) {
                svNVOK = rewriteOneByteOfNv2499_1(62, 'P');
            } else {
                svNVOK = rewriteOneByteOfNv2499_1(63, 'P');
            }
            mHandler.sendEmptyMessage(1);
        }
        if (svNVOK) {
            Log.d(TAG, "svNVOK-----1---->" + svNVOK);
            mHandler.sendEmptyMessage(2);
        } else {
            Log.d(TAG, "svNVOK-----2---->" + svNVOK);
            if (svCount == 1) {
                Log.d(TAG, "svNVOK-----2--1-->svCount == 1");
                saveNV();
                Log.d(TAG, "svNVOK-----2--1-1->svCount == 1");
            } else if (svCount == 2) {
                Log.d(TAG, "svNVOK-----2--2-->svCount == 1");
                mHandler.sendEmptyMessage(3);
            }
        }
    }
    /*15-10-24 gaoshenpan fix hideNavigationBar& make leave time for NVSaved end*/
    
    /*15-10-24 gaoshenpan fix reset mmi_test_fullscreen & removeMessage begin*/
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.d(TAG, "----------->++onPause");
        Settings.Global.putInt(Config.GetActivity().getContentResolver(),
                "mmi_test_fullscreen", 0);
        /*15-10-26 gaoshenpan fix show navigation after NV saved in AutoResultTest begin*/
        Config.GetActivity().getWindow().getDecorView()
        .setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        /*15-10-26 gaoshenpan fix show navigation after NV saved in AutoResultTest end*/
        mHandler.removeMessages(0);
        mHandler.removeMessages(1);
        mHandler.removeMessages(2);
        mHandler.removeMessages(3);
    }
    /*15-10-24 gaoshenpan fix reset mmi_test_fullscreen & removeMessage begin*/

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Fragment#onDestroy()
     */
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
    /*15-10-24 gaoshenpan add hideNavigationBar begin*/
    public void hideNavigationBar() {
        int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                | View.SYSTEM_UI_FLAG_FULLSCREEN;// hide status bar

        if (android.os.Build.VERSION.SDK_INT >= 19) {
            uiFlags |= 0x00001000; // SYSTEM_UI_FLAG_IMMERSIVE_STICKY: hide
                                    // navigation bars - compatibility: building
                                    // API level is lower thatn 19, use magic
                                    // number directly for higher API target
                                    // level
        } else {
            uiFlags |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
        }

        Config.GetActivity().getWindow().getDecorView()
                .setSystemUiVisibility(uiFlags);
    }
    /*15-10-24 gaoshenpan add hideNavigationBar end*/

}
